package org.jeecg.modules.util;

import java.util.*;
import java.util.concurrent.*;

class Case {
    private int id;
    private String province;

    public Case(int id, String province) {
        this.id = id;
        this.province = province;
    }

    public int getId() {
        return id;
    }

    public String getProvince() {
        return province;
    }
}

class Reviewer {
    private int id;
    private String province;

    public Reviewer(int id, String province) {
        this.id = id;
        this.province = province;
    }

    public int getId() {
        return id;
    }

    public String getProvince() {
        return province;
    }
}

class Distributor implements Runnable {
    private Case caseToAssign;
    private List<Reviewer> reviewers;
    private Map<String, Set<Integer>> assignedReviewers;

    public Distributor(Case caseToAssign, List<Reviewer> reviewers, Map<String, Set<Integer>> assignedReviewers) {
        this.caseToAssign = caseToAssign;
        this.reviewers = reviewers;
        this.assignedReviewers = assignedReviewers;
    }

    @Override
    public void run() {
        String caseProvince = caseToAssign.getProvince();
        Set<Integer> assigned = assignedReviewers.getOrDefault(caseProvince, new HashSet<>());

        List<Reviewer> availableReviewers = new ArrayList<>();
        for (Reviewer reviewer : reviewers) {
            if (!reviewer.getProvince().equals(caseProvince) && !assigned.contains(reviewer.getId())) {
                availableReviewers.add(reviewer);
            }
        }

        if (availableReviewers.size() >= 3) {
            Collections.shuffle(availableReviewers); // 随机打乱可用的评审列表
            List<Reviewer> selectedReviewers = availableReviewers.subList(0, 3); // 选择前3个评审

            for (Reviewer selectedReviewer : selectedReviewers) {
                assigned.add(selectedReviewer.getId());
                System.out.println("Case " + caseToAssign.getId() + " from " + caseProvince +
                        " assigned to Reviewer " + selectedReviewer.getId() +
                        " from " + selectedReviewer.getProvince());
            }
            assignedReviewers.put(caseProvince, assigned); // 更新已分配的评审
        } else {
            System.out.println("Not enough available reviewers for Case " + caseToAssign.getId() +
                    " from " + caseProvince);
        }
    }
}

public class CaseAssignment {
    public static void main(String[] args) {
        List<Case> cases = Arrays.asList(
                new Case(1, "Beijing"),
                new Case(2, "Shanghai"),
                new Case(3, "Guangdong"),
                new Case(4, "Beijing"),
                new Case(5, "Shanghai")
        );

        List<Reviewer> reviewers = Arrays.asList(
                new Reviewer(101, "Tianjin"),
                new Reviewer(102, "Shanghai"),
                new Reviewer(103, "Guangdong"),
                new Reviewer(104, "Beijing"),
                new Reviewer(105, "Zhejiang"),
                new Reviewer(106, "Jiangsu"),
                new Reviewer(107, "Fujian"),
                new Reviewer(108, "Shandong")
        );

        Map<String, Set<Integer>> assignedReviewers = new ConcurrentHashMap<>();

        ExecutorService executor = Executors.newFixedThreadPool(cases.size());

        for (Case caseToAssign : cases) {
            executor.execute(new Distributor(caseToAssign, reviewers, assignedReviewers));
        }

        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}